home *** CD-ROM | disk | FTP | other *** search
/ Meeting Pearls 2 / Meeting Pearls Vol. II (1995)(GTI - Schatztruhe)[!].iso / Pearls / gfx / pbm / source / jpegV5.lha / jpegV5 / src / jerror.c < prev    next >
C/C++ Source or Header  |  1994-12-23  |  6KB  |  212 lines

  1. /*
  2.  * jerror.c
  3.  *
  4.  * Copyright (C) 1991-1994, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains simple error-reporting and trace-message routines.
  9.  * These are suitable for Unix-like systems and others where writing to
  10.  * stderr is the right thing to do.  Many applications will want to replace
  11.  * some or all of these routines.
  12.  *
  13.  * These routines are used by both the compression and decompression code.
  14.  */
  15.  
  16. /* this is not a core library module, so it doesn't define JPEG_INTERNALS */
  17. #include "jinclude.h"
  18. #include "jpeglib.h"
  19. #include "jversion.h"
  20.  
  21. #include "jerror.h"        /* get error codes */
  22. #define JMAKE_MSG_TABLE
  23. #include "jerror.h"        /* create message string table */
  24.  
  25. #ifndef EXIT_FAILURE        /* define exit() codes if not provided */
  26. #define EXIT_FAILURE  1
  27. #endif
  28.  
  29.  
  30. /*
  31.  * Error exit handler: must not return to caller.
  32.  *
  33.  * Applications may override this if they want to get control back after
  34.  * an error.  Typically one would longjmp somewhere instead of exiting.
  35.  * The setjmp buffer can be made a private field within an expanded error
  36.  * handler object.  Note that the info needed to generate an error message
  37.  * is stored in the error object, so you can generate the message now or
  38.  * later, at your convenience.
  39.  * You should make sure that the JPEG object is cleaned up (with jpeg_abort
  40.  * or jpeg_destroy) at some point.
  41.  */
  42.  
  43. METHODDEF void
  44. error_exit (j_common_ptr cinfo)
  45. {
  46.   /* Always display the message */
  47.   (*cinfo->err->output_message) (cinfo);
  48.  
  49.   /* Let the memory manager delete any temp files before we die */
  50.   jpeg_destroy(cinfo);
  51.  
  52.   exit(EXIT_FAILURE);
  53. }
  54.  
  55.  
  56. /*
  57.  * Actual output of an error or trace message.
  58.  * Applications may override this method to send JPEG messages somewhere
  59.  * other than stderr.
  60.  */
  61.  
  62. METHODDEF void
  63. output_message (j_common_ptr cinfo)
  64. {
  65.   char buffer[JMSG_LENGTH_MAX];
  66.  
  67.   /* Create the message */
  68.   (*cinfo->err->format_message) (cinfo, buffer);
  69.  
  70.   /* Send it to stderr, adding a newline */
  71.   fprintf(stderr, "%s\n", buffer);
  72. }
  73.  
  74.  
  75. /*
  76.  * Decide whether to emit a trace or warning message.
  77.  * msg_level is one of:
  78.  *   -1: recoverable corrupt-data warning, may want to abort.
  79.  *    0: important advisory messages (always display to user).
  80.  *    1: first level of tracing detail.
  81.  *    2,3,...: successively more detailed tracing messages.
  82.  * An application might override this method if it wanted to abort on warnings
  83.  * or change the policy about which messages to display.
  84.  */
  85.  
  86. METHODDEF void
  87. emit_message (j_common_ptr cinfo, int msg_level)
  88. {
  89.   struct jpeg_error_mgr * err = cinfo->err;
  90.  
  91.   if (msg_level < 0) {
  92.     /* It's a warning message.  Since corrupt files may generate many warnings,
  93.      * the policy implemented here is to show only the first warning,
  94.      * unless trace_level >= 3.
  95.      */
  96.     if (err->num_warnings == 0 || err->trace_level >= 3)
  97.       (*err->output_message) (cinfo);
  98.     /* Always count warnings in num_warnings. */
  99.     err->num_warnings++;
  100.   } else {
  101.     /* It's a trace message.  Show it if trace_level >= msg_level. */
  102.     if (err->trace_level >= msg_level)
  103.       (*err->output_message) (cinfo);
  104.   }
  105. }
  106.  
  107.  
  108. /*
  109.  * Format a message string for the most recent JPEG error or message.
  110.  * The message is stored into buffer, which should be at least JMSG_LENGTH_MAX
  111.  * characters.  Note that no '\n' character is added to the string.
  112.  * Few applications should need to override this method.
  113.  */
  114.  
  115. METHODDEF void
  116. format_message (j_common_ptr cinfo, char * buffer)
  117. {
  118.   struct jpeg_error_mgr * err = cinfo->err;
  119.   int msg_code = err->msg_code;
  120.   const char * msgtext = NULL;
  121.   const char * msgptr;
  122.   char ch;
  123.   boolean isstring;
  124.  
  125.   /* Look up message string in proper table */
  126.   if (msg_code > 0 && msg_code <= err->last_jpeg_message) {
  127.     msgtext = err->jpeg_message_table[msg_code];
  128.   } else if (err->addon_message_table != NULL &&
  129.          msg_code >= err->first_addon_message &&
  130.          msg_code <= err->last_addon_message) {
  131.     msgtext = err->addon_message_table[msg_code - err->first_addon_message];
  132.   }
  133.  
  134.   /* Defend against bogus message number */
  135.   if (msgtext == NULL) {
  136.     err->msg_parm.i[0] = msg_code;
  137.     msgtext = err->jpeg_message_table[0];
  138.   }
  139.  
  140.   /* Check for string parameter, as indicated by %s in the message text */
  141.   isstring = FALSE;
  142.   msgptr = msgtext;
  143.   while ((ch = *msgptr++) != '\0') {
  144.     if (ch == '%') {
  145.       if (*msgptr == 's') isstring = TRUE;
  146.       break;
  147.     }
  148.   }
  149.  
  150.   /* Format the message into the passed buffer */
  151.   if (isstring)
  152.     sprintf(buffer, msgtext, err->msg_parm.s);
  153.   else
  154.     sprintf(buffer, msgtext,
  155.         err->msg_parm.i[0], err->msg_parm.i[1],
  156.         err->msg_parm.i[2], err->msg_parm.i[3],
  157.         err->msg_parm.i[4], err->msg_parm.i[5],
  158.         err->msg_parm.i[6], err->msg_parm.i[7]);
  159. }
  160.  
  161.  
  162. /*
  163.  * Reset error state variables at start of a new image.
  164.  * This is called during compression startup to reset trace/error
  165.  * processing to default state, without losing any application-specific
  166.  * method pointers.  An application might possibly want to override
  167.  * this method if it has additional error processing state.
  168.  */
  169.  
  170. METHODDEF void
  171. reset_error_mgr (j_common_ptr cinfo)
  172. {
  173.   cinfo->err->num_warnings = 0;
  174.   /* trace_level is not reset since it is an application-supplied parameter */
  175.   cinfo->err->msg_code = 0;    /* may be useful as a flag for "no error" */
  176. }
  177.  
  178.  
  179. /*
  180.  * Fill in the standard error-handling methods in a jpeg_error_mgr object.
  181.  * Typical call is:
  182.  *    struct jpeg_compress_struct cinfo;
  183.  *    struct jpeg_error_mgr err;
  184.  *
  185.  *    cinfo.err = jpeg_std_error(&err);
  186.  * after which the application may override some of the methods.
  187.  */
  188.  
  189. GLOBAL struct jpeg_error_mgr *
  190. jpeg_std_error (struct jpeg_error_mgr * err)
  191. {
  192.   err->error_exit = error_exit;
  193.   err->emit_message = emit_message;
  194.   err->output_message = output_message;
  195.   err->format_message = format_message;
  196.   err->reset_error_mgr = reset_error_mgr;
  197.  
  198.   err->trace_level = 0;        /* default = no tracing */
  199.   err->num_warnings = 0;    /* no warnings emitted yet */
  200.   err->msg_code = 0;        /* may be useful as a flag for "no error" */
  201.  
  202.   /* Initialize message table pointers */
  203.   err->jpeg_message_table = jpeg_message_table;
  204.   err->last_jpeg_message = (int) JMSG_LASTMSGCODE - 1;
  205.  
  206.   err->addon_message_table = NULL;
  207.   err->first_addon_message = 0;    /* for safety */
  208.   err->last_addon_message = 0;
  209.  
  210.   return err;
  211. }
  212.